<?php

namespace app\modules\api\controllers;

use app\models\AuthOrder;
use app\models\BuyerCashback;
use app\models\BuyerCashbackPic;
use app\models\BuyerQr;
use app\modules\api\CommonController;
use app\models\MiniprogramSession;
use app\models\User;
use Yii;


class UserController extends CommonController
{
    //订单是否存在
    public function actionSearchOrder(){
        $order_no = Yii::$app->request->post('order_no', '');
        if(empty($order_no)) throw new \Exception('请输入订单号',1);
        $authOrder = AuthOrder::findOne(['order_no'=>$order_no]);
        if(empty($authOrder)) throw new \Exception('订单不存在',1);
        return $this->success([]);
    }
    //订单数据
    public function actionOrderData(){
        $order_no = Yii::$app->request->post('order_no', '');
        if(empty($order_no)) throw new \Exception('请输入订单号',1);
        $authOrder = AuthOrder::findOne(['order_no'=>$order_no]);
        if(empty($authOrder)) throw new \Exception('订单不存在',1);
        return $this->success([
            'order_no'=> $order_no,
            'content'=> $authOrder->getCashbackIntro(),
//            'content'=> '24小时确认收货，五星好评，评论字数10字以上，评价截图上传2张以上，返现2元！有问题请直接联系客服！',
        ]);
    }
    //用户申请
    public function actionApply(){
        $query = BuyerCashback::find();
        $query->andWhere(['buyer_id'=>$this->user->id]);
        $count = $query->count();
        if($count<1){
            return $this->success(['total'=>0,'page_nums'=>0,'rows'=>[]]);
        }
        $objs = $query->all();
        $rows = [];
        foreach ($objs as $obj){
            $row = $obj->toArray();
            $row['auth_shop_name'] = $obj->authShop?$obj->authShop->name:'';
            $row['ctime2'] = date('Y-m-d H:i:s',$obj->ctime);
            $rows[] = $row;
        }
        return $this->success(['total'=>intval($count),'page_nums'=>0,'rows'=>$rows]);
    }
    //用户申请保存
    public function actionApplySave(){
        $order_no = Yii::$app->request->post('order_no', 0);
        $user_account = Yii::$app->request->post('user_account', 0);
        $user_realname = Yii::$app->request->post('user_realname', 0);
        $content = Yii::$app->request->post('content', 0);
        $upload_ids = Yii::$app->request->post('upload_ids', 0);
        if(empty($order_no)) throw new \Exception('订单号不能为空',1);
        if(empty($user_account)) throw new \Exception('账号不能为空',1);
        if(empty($user_realname)) throw new \Exception('姓名不能为空',1);
        if(empty($upload_ids)) throw new \Exception('图片不能为空',1);
        $authOrder = AuthOrder::findOne(['order_no'=>$order_no]);
        if(empty($authOrder)) throw new \Exception('无效订单',1);

        $buyerCashback = new BuyerCashback();
        $buyerCashback->buyer_id = $this->user->id;
        $buyerCashback->auth_shop_id = $authOrder->auth_shop_id;
        $buyerCashback->auth_order_id = $authOrder->id;
        $buyerCashback->order_no = $order_no;
        $buyerCashback->user_account = $user_account;
        $buyerCashback->user_realname = $user_realname;
        $buyerCashback->content = $content;
        $buyerCashback->ctime = time();
        $buyerCashback->state = BuyerCashback::STATE_CHECKING;
        if(!$buyerCashback->save())throw new \Exception(json_encode($buyerCashback->getErrors(),JSON_UNESCAPED_UNICODE),1);
        $upload_ids = explode(',',$upload_ids);
        foreach($upload_ids as $upload_id){
            $buyerCashbackPic = new BuyerCashbackPic();
            $buyerCashbackPic->buyer_cashback_id = $buyerCashback->id;
            $buyerCashbackPic->pic_id = $upload_id;
            $buyerCashbackPic->save();
        }
        return $this->success();
    }
}
